<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Photo extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
    }
	

    public function index()
    {
        //$this->input->post();
        $error = array();
        
        //提交处理
        if ($this->input->is_post()) 
        {
            // 机构中的个人信息
            $that_userid = _get_key_val($this->input->get('agid'), true);
            $that_usertype = $this->input->get('agusertype');
            // 跳转页面默认为管理旗下艺人
            $info_url = '/m/info?agid='.$this->input->get('agid').'&agusertype='.$this->input->get('agusertype'); 
            $url = '/m/works?agid='.$this->input->get('agid');
		
            if(empty($that_userid)) {
                // 登陆用户
                $that_userid = $this->thatUser['id'];
                $that_usertype = $this->thatUser['usertype'];
                $url = '/m/fans';
            }
            
            //上传原图
            $path = "./upload/userlogo/".date('Ym', time()).'/';
            $this->_make_directory($path); //没有就创建
            $pic_config['upload_path'] = $path;
            $pic_config['allowed_types'] = 'gif|jpg|png';
            $pic_config['file_name'] = 'userlogo_'.$this->loginID.'_'.time().'.jpg'; //重新命名
            
            $this->load->library('upload', $pic_config);
            if(!$this->upload->do_upload('preimg')) 
            {
                $error[] = $this->upload->display_errors();
            }
            
            //图片处理
            $img_data = $this->upload->data();
            //1. 缩小
            $pic_config['image_library'] = 'gd2';
            $pic_config['source_image'] = $img_data['full_path'];
            $pic_config['maintain_ratio'] = TRUE;
            $pic_config['width'] = 422;  //头像框中最大宽度的像素
            $pic_config['height'] = 422; //头像框中最大高度的像素
            $this->load->library('image_lib', $pic_config);
            if(!$this->image_lib->resize()) {
                $error[] = $this->image_lib->display_errors();
            }
                       
            //2. 裁剪  
            $img_config['source_image'] = $img_data['full_path']; //全路径
            $img_config['x_axis'] = $this->input->post('left'); 
            $img_config['y_axis'] = $this->input->post('top'); 
            $img_config['width'] = $this->input->post('right') - $this->input->post('left'); //宽度
            $img_config['height'] = $this->input->post('bottom') - $this->input->post('top'); //高度
            $img_config['maintain_ratio'] =  false;
            
            $this->image_lib->initialize($img_config);
            if (!$this->image_lib->crop())
            {
                $error[] =  $this->image_lib->display_errors();
                alert_err($error, current_url()."?{$_SERVER['QUERY_STRING']}");
            } else {
                
                //裁剪后上传OSS
                $this->upload->cropUpload();
                
                $data = array(
                    #'userlogo' => $path.'/'.$pic_config['file_name']
                    'userlogo' => ((substr($path,-1)==='/')?$path:'/').$pic_config['file_name']
                );

                //头像保存到数据库
                $this->User_model->update_info_by_id($that_userid, $data);

                $agid = $this->input->get('agid');
                if (isset($agid) && $agid)
                {
                    alert('保存成功', $url);
                }

                $flag = $this->input->get('flag');
                if (isset($flag) && $flag)
                {
                    redirect($info_url);
                }
                redirect($url);
//             alert('修改成功', $url);

            }
        }
        
        $result = array(
           'error' => $error 
        );

        //$flag参数只是用来区分用户是点击了 菜单页上的头像设置(模板:menu_photo), 还是内页的 头像设置(模板:info_photo)
        $flag = $this->input->get('flag');
        if (isset($flag) && $flag)
        {
        	$loginFlag = FALSE; // 用户判断是否修改登陆账号
        	$data_body = array(); // 初始化
        	
        	$that_userid = _get_key_val($this->input->get('agid'), true);
        	$that_usertype = $this->input->get('agusertype');
        	
        	if ( ! $that_userid || ! $that_usertype )
        	{
        		$that_userid = $this->thatUser['id'];
        		$that_usertype = $this->thatUser['usertype'];
        		$loginFlag = TRUE; // 判断是修改登陆账户
        	}
        	// 用户链接
        	$infoUrl = (TRUE == $loginFlag) ? '/m/info/' : '/m/info?agid=' . _get_key_val($that_userid) . '&agusertype=' . $that_usertype;
        	$picUrl = (TRUE == $loginFlag) ? '/m/info/personalPic' : '/m/info/personalPic?agid=' . _get_key_val($that_userid) . '&agusertype=' . $that_usertype;
        	$avatarUrl = (TRUE == $loginFlag) ? '/m/photo/' : '/m/photo?agid=' . _get_key_val($that_userid) . '&agusertype=' . $that_usertype;
        	$workUrl = (TRUE == $loginFlag) ? '/m/works' : '/m/works?agid=' . _get_key_val($that_userid);
        	
        	$result['hasAvatar'] = $loginFlag;
        	$result['infoUrl'] = $infoUrl;
        	$result['picUrl'] = $picUrl;
        	$result['avatarUrl'] = $avatarUrl;
        	$result['workUrl'] = $workUrl;
        	$result['usertype'] = $that_usertype;
        	$this->load->view('m/info_photo',$result);
            return '';
        }
        $this->load->view('m/menu_photo', $result);
    }
    
    
    private function _make_directory($directoryName) {
        $directory_name = str_replace("\\","/",$directoryName);
        $dirNames = explode('/', $directory_name);
        $total = count($dirNames) ;
        $temp = '';
        for($i=0; $i<$total; $i++) {
            $temp .= $dirNames[$i].'/';
            if (!is_dir($temp)) {
                $oldmask = umask(0);
                if (!mkdir($temp, 0777)) exit("不能建立目录 $temp"); 
                umask($oldmask);
            }
        }
        return true;
    }

}
